package com.zhentao.controller;

import com.zhentao.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileWriter;
import java.util.*;


@Controller
public class Test02Controller {
    @Autowired
    private Configuration configuration;

    @ResponseBody
    @RequestMapping("/createHtml")
    public String createHtml() throws Exception {
        Template template = configuration.getTemplate("02-test.ftl");

        Student student = new Student();
        student.setName("刘佳惠");
        student.setAge(20);

        Student student1 = new Student();
        student1.setName("张婉莹");
        student1.setAge(20);
        //list
        List<Object> list = new ArrayList<>();
        list.add(student);
        list.add(student1);

        Map<String, Object> map = new HashMap<>();
        map.put("studentList", list);
        map.put("student1", student);

        template.process(map, new FileWriter("D://03.html"));
        return "success";
    }
}
